/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.cesupa.cabcc.facade;

import br.cesupa.cabcc.dao.CargoDAO;
import br.cesupa.cabcc.entity.Cargo;
import br.cesupa.cabcc.factory.FactoryDAO;
import java.util.List;

/**
 *
 * @author yuri
 */
public class CargoFacade {

    private static CargoDAO cDAO = FactoryDAO.getCargoSUDI();
    private static List<Cargo> listCargos = getAllCargos();

    private static List<Cargo> getAllCargos() {
        List<Cargo> aux = cDAO.getAll();
        return aux;
    }

    private static void atualizaListCargos(){
        listCargos = getAllCargos();
    }

    public static Cargo cadastrarCargo(Cargo cargo) {
        cDAO.salvar(cargo);
        cargo = cDAO.getByNome(cargo.getNome());
        if (cargo != null) {
            atualizaListCargos();
        }
        return cargo;
    }

    public static void atualizarCargo(Cargo cargo) {
        cDAO.atualizar(cargo);
        atualizaListCargos();
    }

    public static void excluirCargo(Cargo cargo){
        cDAO.remover(cargo);
        atualizaListCargos();
    }

    public static Cargo getCargoPorNome(String nome) {
        if (listCargos != null) {
            for (Cargo c : listCargos) {
                if (c.getNome().equals(nome)) {
                    return c;
                }
            }
        }
        return null;
    }

    /**
     * @return the listCargos
     */
    public static List<Cargo> getListCargos() {
        return listCargos;
    }
}
